package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/19
 * @description:
 * @version:1.0
 */
@RestController

@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService service;
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
     try {
              service.editNumberByDate(orderSetting);
              return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
             }catch (Exception e){
                 e.printStackTrace();
                 return new Result(false,MessageConst.ORDERSETTING_FAIL);
             }
    }
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map>list=  service.getOrderSettingByMonth(date);
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
         try {
             //解析excelFile文件
             List<String[]> list = POIUtils.readExcel(multipartFile);
             //判空
             if (list!=null&&list.size()>0){
                 //封装数据 因为service要的是List<OrderSetting>类型的数据
                List<OrderSetting> orderSettings = new ArrayList<>();
                //循环 封装数据
                for (String[] rowdatas : list) {
                    //判断这一行是否为空
                 if (rowdatas[0]==null){
                    continue;
                 }
                    OrderSetting OrderSetting = new OrderSetting();
                    //把读取到时间设置给ordersettering
                    OrderSetting.setOrderDate(new Date(rowdatas[0]));
                    //把读取到的可预约人数设置给ordersettering
                    OrderSetting.setNumber(Integer.parseInt(rowdatas[1]));
                   // 把ordersettering添加到list集合中
                    orderSettings.add(OrderSetting);
                }
                //调用service保存数据库
                 service.add(orderSettings);
                return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
             }else {
                 throw new RuntimeException("数据不能为空");
             }
         }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.IMPORT_ORDERSETTING_FAIL);
                 }
    }

}
